package com.snail.snailvr.views;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.interfaces.ScreenSwitchListener;
import com.snail.snailvr.utils.IntentUtils;
import com.snail.snailvr.utils.ScreenSwitchUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;

public class PlayHintActivity extends BaseToolbarActivity implements ScreenSwitchListener{

    private static final String PATH_KEY = "path_key";
    private static final String NAME_KEY = "name_key";
    private static final String ISLIVE_KEY = "islive_key";

    private String PATH;
    private String NAME;
    private boolean ISLIVE;

    @Bind(R.id.play_now)
    TextView play_now;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.count_number)
    ImageView count_number;

    @Bind(R.id.countPanel)
    FrameLayout countPanel;

    @Bind(R.id.hintPanel)
    RelativeLayout hintPanel;

    private ScreenSwitchUtils instance;

    private boolean shouldJump = true;

    private Animation number_animation;

    private Subscription sub;
    private Subscription sub2;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (instance.isPortrait() && newConfig.orientation==Configuration.ORIENTATION_PORTRAIT) {
            // 竖屏
        }
        else {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // 横屏
            hintPanel.setVisibility(View.GONE);
            play_now.setVisibility(View.GONE);
            toolbar.setVisibility(View.GONE);

            countPanel.setVisibility(View.VISIBLE);

            if (shouldJump) {
                shouldJump = false;

                final int countTime = 5;
                sub = Observable.interval(0, 1, TimeUnit.SECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(new Func1<Long, Integer>() {
                            @Override
                            public Integer call(Long increaseTime) {
                                return countTime - increaseTime.intValue();
                            }
                        })
                        .take(countTime + 1).subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {

                        if(integer == 5) {
                            count_number.setImageResource(R.drawable.count_five);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 4) {
                            count_number.setImageResource(R.drawable.count_four);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 3) {
                            count_number.setImageResource(R.drawable.count_three);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 2) {
                            count_number.setImageResource(R.drawable.count_two);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 1) {
                            count_number.setImageResource(R.drawable.count_one);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 0) {
                            // 跳转
                            IntentUtils.playRealVideo(PlayHintActivity.this, PATH, NAME, false);

                        }
                    }
                });

            }
        }
    }

    public static Intent getIntent(Context context, String path, String name){
        Intent intent = new Intent(context, PlayHintActivity.class);
        intent.putExtra(PATH_KEY, path);
        intent.putExtra(NAME_KEY, name);
        return intent;
    }

    @Override
    public void onCreate(Bundle savedInstance) {

        super.onCreate(savedInstance);

        instance = ScreenSwitchUtils.init(this.getApplicationContext());

        Configuration cfg = getResources().getConfiguration();

        if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // 横屏
            hintPanel.setVisibility(View.GONE);
            play_now.setVisibility(View.GONE);
            toolbar.setVisibility(View.GONE);

            countPanel.setVisibility(View.VISIBLE);

            if (shouldJump)
            {
                shouldJump = false;

                final int countTime = 5;

                sub2 = Observable.interval(0, 1, TimeUnit.SECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(new Func1<Long, Integer>() {
                            @Override
                            public Integer call(Long increaseTime) {
                                return countTime - increaseTime.intValue();
                            }
                        })
                        .take(countTime + 1).subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {

                        if(integer == 5) {
                            count_number.setImageResource(R.drawable.count_five);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 4) {
                            count_number.setImageResource(R.drawable.count_four);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 3) {
                            count_number.setImageResource(R.drawable.count_three);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 2) {
                            count_number.setImageResource(R.drawable.count_two);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 1) {
                            count_number.setImageResource(R.drawable.count_one);
                            count_number.startAnimation(number_animation);
                        }
                        else if(integer == 0) {
                            // 跳转
                            IntentUtils.playRealVideo(PlayHintActivity.this, PATH, NAME, false);

                        }
                    }
                });
            }
        }
        else {
            // 竖屏
        }
    }

    @Override
    protected boolean onToolbarMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    protected void handleIntent() {
        PATH = getIntent().getStringExtra(PATH_KEY);
        NAME = getIntent().getStringExtra(NAME_KEY);
        ISLIVE = getIntent().getBooleanExtra(ISLIVE_KEY, false);
    }

    @Override
    protected void initView() {

        setupToolbar(toolbar, getResources().getString(R.string.back), -1);

        play_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (shouldJump) {
                    shouldJump = false;
                    IntentUtils.playRealVideo(PlayHintActivity.this, PATH, NAME, true);

                }
            }
        });

        countPanel.setVisibility(View.GONE);
        number_animation = AnimationUtils.loadAnimation(this, R.anim.counting_in_out);
        number_animation.setRepeatCount(0);//设置重复次数
        number_animation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_play_hint;
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        instance.start(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        instance.stop();
    }

    @Override
    protected void onDestroy(){
        if(sub!=null && !sub.isUnsubscribed()){
            sub.unsubscribe();
        }
        if(sub2!=null && !sub2.isUnsubscribed()){
            sub2.unsubscribe();
        }

        instance.destroy();

        super.onDestroy();
    }

    @Override
    public void onLandScape() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    public void onReverseLandScape() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    }

    @Override
    public void onPortrait() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    public void onReversePortrait() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    }
}



